home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / PixMapToPICT.c < prev    next >
Text File  |  1995-07-26  |  4KB  |  108 lines

  1. /*
  2. PixMapToPICT.c
  3. Saves a section of a PixMap (or BitMap) as a PICT file. You may substitute a
  4. BitMap handle for the PixMap handle. You are allowed to specify the PICT's pixel
  5. size and the color table used in translating the PixMap to a PICT. If you
  6. request zero pixelSize, the PICT's pixelSize will be the same as that of your
  7. PixMap. If the supplied color table handle is NULL then the Macintosh operating
  8. system default color table for that pixel size will be used. Note that PICT
  9. files include a color table. If you want to preserve the numerical value of 
  10. each pixel then set pixelSize to zero (i.e. unchanged) and supply the color 
  11. table of your PixMap, i.e. (**pm).pmTable.
  12.  
  13. EXAMPLE:
  14.     PixMapToPICT("image.pict",((CWindowPtr)window)->portPixMap,&window->portRect,0,NULL);
  15.  
  16. BASED ON:
  17. Inside Macintosh: Imaging With QuickDraw, page 7-11
  18. Ortiz, G.A., and Johnson, C. (1994) A space-saving PICT trick. develop 20,63.
  19.  
  20. HISTORY:
  21. 9/28/93    mike schechter wrote it, based in part on PixMapToPostScript.c
  22. 9/29/93    dgp polished it, substituting a GWorld for a CPort, 
  23.             so we can specify the pixel size and color table.
  24. 10/2/93    dgp    added pixelSize and colorTable arguments.
  25. 9/5/94 dgp removed assumption in printf's that int==short.
  26. 6/30/95 dgp updated it to use OpenCPicture().
  27. */
  28. #define NEW_DEVICE 0
  29. #define DEBUG 1
  30.  
  31. #include "VideoToolbox.h"
  32. //#include <Errors.h>
  33. PicHandle PixMapToPicture(PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable);
  34.  
  35. void PixMapToPICT(char *filename,PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable)
  36. {
  37.     long buffer[128],n;
  38.     int i;
  39.     FILE *file;
  40.     PicHandle pic;
  41.     
  42.     pic=PixMapToPicture(pm,rectPtr,pixelSize,cTable);
  43.  
  44.     // save Picture as a PICT file
  45.     file=fopen(filename,"wb");
  46.     if(file==NULL)PrintfExit("%s %d: Error in opening file \"%s\".\n",__FILE__,__LINE__
  47.         ,filename);
  48.     // zero 512-byte header, because Apple says so.
  49.     for(i=0;i<128;i++)buffer[i]=0;
  50.     if(128!=fwrite(buffer,4,128,file))
  51.         PrintfExit("%s %d: Error writing header of file \"%s\".\n",__FILE__,__LINE__,filename);
  52.     n=GetHandleSize((Handle)pic);
  53.     n++;n-=n%2; /* round up to multiple of 2 */
  54.     HLock((Handle)pic);
  55.     if(n!=fwrite(*pic,1,n,file))
  56.         PrintfExit("%s %d: Error writing file \"%s\"\n",__FILE__,__LINE__,filename);
  57.     fclose(file);
  58.     SetFileInfo(filename,'PICT','ttxt');
  59.  
  60.     KillPicture(pic);
  61. }
  62.  
  63. PicHandle PixMapToPicture(PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable)
  64. {
  65.     long value;
  66.     int error;
  67.     PicHandle pic;
  68.     CGrafPtr oldPort;
  69.     GDHandle oldDevice;
  70.     GWorldPtr world;
  71.     GWorldFlags flags;
  72.     OpenCPicParams params;
  73.     
  74.     Gestalt(gestaltQuickdrawVersion,&value);
  75.     if(value<gestalt32BitQD)
  76.         PrintfExit("%s %d: Require 32-bit QuickDraw.\n",__FILE__,__LINE__);
  77.     if(pixelSize==0)if((**pm).rowBytes & 0x8000)    // Pixmap or Bitmap?
  78.         pixelSize=(**pm).pixelSize;        // Pixmap
  79.     else pixelSize=1;                    // Bitmap
  80.     GetGWorld(&oldPort,&oldDevice);
  81.     flags=0;
  82.     error=NewGWorld(&world,pixelSize,rectPtr,cTable,NULL,flags|useTempMem);
  83.     if(error)error=NewGWorld(&world,pixelSize,rectPtr,cTable,NULL,flags);
  84.     if(error==cTempMemErr || error==cNoMemErr || error==memFullErr)
  85.         PrintfExit("%s %d: not enough memory; reduce rect or pixelSize.\n",__FILE__,__LINE__);
  86.     if(error)PrintfExit("%s %d: NewGWorld error %d.\n",__FILE__,__LINE__,error);
  87.     LockPixels(GetGWorldPixMap(world));
  88.     SetGWorld(world,NULL);
  89.     params.srcRect=*rectPtr;    // best rectangle for displaying this picture
  90.     params.hRes= 0x00480000;    // horizontal resolution
  91.     params.vRes= 0x00480000;    // vertical resolution
  92.     params.version=-2;            // always set this field to -2
  93.     params.reserved1=0;            // this field is unused
  94.     params.reserved2=0;            // this field is unused
  95.     CopyBitsQuickly((BitMap *)*pm,(BitMap *)*GetGWorldPixMap(world),rectPtr,rectPtr,srcCopy,NULL);
  96.     pic=OpenCPicture(¶ms);    // start creating the picture
  97.     ClipRect(rectPtr);
  98.     CopyBits((BitMap *)*GetGWorldPixMap(world),(BitMap *)*GetGWorldPixMap(world),rectPtr,rectPtr,srcCopy,NULL);
  99.     ClosePicture();
  100.     SetGWorld(oldPort,oldDevice);
  101.     DisposeGWorld(world);    
  102.     error=QDError();
  103.     if(error)PrintfExit("%s %d: QDError %d.\n",__FILE__,__LINE__,(int)QDError());
  104.     if(EmptyRect(&(*pic)->picFrame) && !EmptyRect(rectPtr))
  105.         PrintfExit("%s %d: out of memory. Reduce rect or pixelSize.\n",__FILE__,__LINE__);
  106.     return pic;
  107. }
  108.